package com.petiot.rabbitmq;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	protected String pwd;
	protected String number;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		Button button = (Button) findViewById(R.id.login);
		Button exit_button = (Button)findViewById(R.id.exit);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		//setup_Connection();
		
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				number = ((EditText) findViewById(R.id.editText1)).getText().toString();
				pwd = ((EditText) findViewById(R.id.editText2)).getText().toString();
				System.out.println("number = " + number + "pwd = " + pwd);
				setup_Connection();
			}
		});
		exit_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	Thread connectionThread ;
	
	private void setup_Connection() {
		// TODO Auto-generated method stub
		connectionThread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Log.v("2FFFF2", "2DDDDD2");
				Intent intent = new Intent(MainActivity.this, PushService.class);
				Bundle bundle = new Bundle();
				bundle.putString("number", number);
				bundle.putString("pwd", pwd);
				intent.putExtras(bundle);
				startService(intent);
				
			}
		});
		connectionThread.start();
	}
	
	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if (intent.getAction().equals("login_succsess")){
				Intent intent2 = new Intent(MainActivity.this, ContorlPage.class);		
				startActivity(intent2);
				finish();
			} else if (intent.getAction().equals("login_failure")){
				Toast.makeText(getApplicationContext(), "AMQP链接失败,请重新登录", Toast.LENGTH_SHORT).show();
			}
		}
		
	};
	
	protected void  onResume() {
		super.onResume();
		IntentFilter filter = new IntentFilter();
		filter.addAction("login_succsess");
		registerReceiver(receiver, filter);
		
		IntentFilter filter1 = new IntentFilter();
		filter1.addAction("login_failure");
		registerReceiver(receiver, filter1);
	}
	
	protected void onDestroy() {
		super.onDestroy();
		connectionThread.interrupt();
		unregisterReceiver(receiver);
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
